﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;

namespace SharpPrinter
{
    class Program
    {
        static void Main(string[] args)
        {
            GetInstalledPrinterTCPIPPort();
        }

        public static void GetInstalledPrinterDriverWithoutInstance()
        {

            //Create a new ConnectionOptions object to grand privileges 
            //and impersonation of the current user
            ConnectionOptions connection = new ConnectionOptions();
            connection.EnablePrivileges = true;
            connection.Impersonation = ImpersonationLevel.Impersonate;


            //Define a new scope and hand over connection options (e.g. User/Password)
            ManagementScope scope = new ManagementScope(
            "\\\\" + "." + "\\root\\CIMV2", connection);



            //Specify the query,faster as we use a special printer and don't query each of them
            ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_PrinterDriver");

            //Create a management object
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

            ManagementObjectCollection oReturnCollection = searcher.Get();

            foreach (ManagementObject queryObj in oReturnCollection)
            {
                string driverName = queryObj["Name"].ToString();
                //The Driver name for this printer. This is a compound key composed of the Name, Version, and SupportedPlatform values
                char sep = ',';
                string[] nameArr = driverName.Split(sep);
                string reallyName = nameArr[0];
                Console.WriteLine(reallyName);
                //_installedPrinterDriverWithoutInstance.Add(queryObj);
                //Logger.WriteInfo(this, string.Format("Installed Printer Driver Without Instance: {0}", queryObj["Name"]));
            }
        }

        public static void GetInstalledPrinterTCPIPPort()
        {
            //Create a new ConnectionOptions object to grand privileges 
            //and impersonation of the current user
            ConnectionOptions connection = new ConnectionOptions();
            connection.EnablePrivileges = true;
            connection.Impersonation = ImpersonationLevel.Impersonate;

            //Define a new scope and hand over connection options (e.g. User/Password)
            ManagementScope scope = new ManagementScope(
            "\\\\" + "." + "\\root\\CIMV2", connection);

            //Specify the query,faster as we use a special printer and don't query each of them
            ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_TCPIPPrinterPort");

            //Create a management object
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

            ManagementObjectCollection oReturnCollection = searcher.Get();

            foreach (ManagementObject queryObj in oReturnCollection)
            {
                string portName = queryObj["Name"].ToString();
                Console.WriteLine(portName);
            }
        }
    }
}
